package com.wing.productionPlan.controller.admin;


import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.entity.ProProductDeliveryOrder;
import com.wing.productionPlan.model.entity.ProProductDeliveryOrderDetail;
import com.wing.productionPlan.model.response.ProProductDeliveryOrderDetailVo;
import com.wing.productionPlan.model.response.ProProductDeliveryOrderVo;
import com.wing.productionPlan.service.ProProductDeliveryOrderDetailService;
import com.wing.productionPlan.service.ProProductDeliveryOrderService;
import com.wing.web.excel.DemandPlanExcelln;
import com.wing.web.excel.DownExcelUtil;
import com.wing.web.excel.ExcelUtil;
import com.wing.web.excel.ProProductOrderExcelln;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.*;

/**
 * 提货订单
 *
 * @author 25923
 */
@Slf4j
@Validated
@Api(value = "Admin-提货订单详情", tags = "#/product/delivery/orderDetailInfo")
@RestController("proProductDeliveryOrderDetailController")
@RequestMapping("delivery/admin/orderDetailInfo")
@RequiredArgsConstructor
public class ProProductDeliveryOrderDetailController {

    @Autowired
    private ProProductDeliveryOrderDetailService proProductDeliveryOrderDetailService;

    @ApiOperation(value = "查询提货订单列表", notes = "搜索-提货订单")
    @GetMapping("/list")
    public PageResult<ProProductDeliveryOrderDetailVo> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            String orderNo, Long customerId, String customerName, String purchaseCode,
            String purchaseName, String skuCode, String status) {
        Page<ProProductDeliveryOrderDetailVo> pg = new Page<>(page, limit);
        Map map = new HashMap();
        map.put("orderNo", orderNo);
        map.put("customerId", customerId);
        map.put("customerName", customerName);
        map.put("purchaseCode", purchaseCode);
        map.put("purchaseName", purchaseName);
        map.put("skuCode", skuCode);
        map.put("status", status);
        Page<ProProductDeliveryOrderDetailVo> list = proProductDeliveryOrderDetailService.pageList4app(pg, map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加提货订单详情", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")

    public JsonResult add(@Validated(ProProductDeliveryOrderDetail.class) ProProductDeliveryOrderDetail proProductDeliveryOrder) {
        if (proProductDeliveryOrderDetailService.add(proProductDeliveryOrder) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    /**
     * 下载产品数据录入excel模板
     */
    @ApiOperation(value = "下载提货信息模板")
    @GetMapping("/template")
    public void template(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String[] headerArr = {"物料简码","组件描述","生产线","采购组件描述","提货数量"};
        List<String[]> downData = new ArrayList<>();
        DownExcelUtil.createExcelTemplate(response, request, "提货信息模板", Arrays.asList(headerArr),downData, null);
    }


    @ApiOperation(value = "导入excel")
    @PostMapping("/importExcel")
    public JsonResult importExcel(HttpServletRequest request, @RequestParam("file") MultipartFile file, String orderNo, LocalDate deliveryTime) {
        if (file.isEmpty()) {
            return JsonResult.fail("上传文件为空！");
        }
        try {
            //获取原始的文件名
            ExcelUtil<ProProductOrderExcelln> reader = new ExcelUtil<>();
            InputStream is = file.getInputStream();
            List<ProProductOrderExcelln> list = reader.read(is,ProProductOrderExcelln.class);

            if (CollectionUtils.isEmpty(list)) {
                return JsonResult.fail("请在表格中添加数据！");
            }
             return proProductDeliveryOrderDetailService.importExcel(list,orderNo,deliveryTime);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("上传文件异常！异常信息:", e);
            return JsonResult.exception("上传文件异常！异常信息:" + e.getMessage());
        }
    }

    @ApiOperation(value = "修改提货订单详情", notes = "修改")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/update")
    public JsonResult update(@Validated(ProProductDeliveryOrderDetail.class) ProProductDeliveryOrderDetail proProductDeliveryOrder) {
        if (proProductDeliveryOrderDetailService.updateInfo(proProductDeliveryOrder) > 0) {
            return JsonResult.success("修改成功");
        }
        return JsonResult.fail("修改失败");
    }

}
